package com.irdstudio.efp.flow.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.efp.flow.service.dao.InstFlowInfoDao;
import com.irdstudio.efp.flow.service.domain.InstFlowInfo;
import com.irdstudio.efp.flow.service.facade.InstFlowInfoService;
import com.irdstudio.efp.flow.service.vo.InstFlowInfoVO;
import com.irdstudio.basic.framework.core.base.FrameworkService;

/**
 * 
 * 
 * 
 * @author Cytus_
 * @since 2018年5月8日 下午8:11:16
 * @version 1.0
 *
 */
@Service("instFlowInfoService")
public class InstFlowInfoServiceImpl implements InstFlowInfoService, FrameworkService {
	
	
	private static Logger logger = LoggerFactory.getLogger(InstFlowInfoServiceImpl.class);
	
	
	@Autowired
	private InstFlowInfoDao instFlowInfoDao;
	
	
	
	public int insertInstFlowInfo(InstFlowInfoVO vo) {
		logger.debug("新增数据开始......");
		int num = 0;
		InstFlowInfo info = beanCopy(vo, new InstFlowInfo());
		try {
			num = this.instFlowInfoDao.insertInstFlowInfo(info);
			logger.debug("新增数据结束......");
		} catch (Exception e) {
			logger.error("新增数据出现异常!", e);
			num = -1;
		}
		return num;
	}



	@Override
	public InstFlowInfoVO queryByFlowInstanceId(String instFlowId) {
		logger.debug("根据流程实例编号"+ instFlowId +"查询流程信息开始......");
		try {
			InstFlowInfo info = instFlowInfoDao.queryByFlowInstanceId(instFlowId);
			InstFlowInfoVO vo = beanCopy(info, new InstFlowInfoVO());
			logger.debug("根据流程实例编号"+ instFlowId +"查询流程信息结束......");
			return vo;
		} catch (Exception e) {
			logger.error("根据流程实例编号"+ instFlowId +"查询流程信息出现异常!", e);
		}
		return null;
	}



    public int updateFlowStatusByFlowInstanceId(InstFlowInfoVO vo) {
        logger.debug("流程实例ID："+ vo.getInstFlowId() +"修改流程状态为："+ vo.getInstFlowState() +"开始......");
        int num = 0;
        InstFlowInfo info = beanCopy(vo, new InstFlowInfo());
        try {
            num = this.instFlowInfoDao.updateFlowStatusByFlowInstanceId(info);
            logger.debug("流程实例ID："+ vo.getInstFlowId() +"修改流程状态为："+ vo.getInstFlowState() +"结束......");
        } catch (Exception e) {
            logger.debug("流程实例ID："+ vo.getInstFlowId() +"修改流程状态为："+ vo.getInstFlowState() +"出现异常!", e);
            num = -1;
        }
        return num;
    }

	@Override
	public int queryErrorTaskCountByInstFlowId(String bizSerno) {
		if (StringUtils.isEmpty(bizSerno)) return 0;
		logger.info("查询异常总数的业务流水号:" + bizSerno);
		int flag = instFlowInfoDao.queryExceptionTaskCount(bizSerno);
		return flag;
	}

}
